import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:videotop/base_model.dart';
import 'package:videotop/jsonbean/login_bean.dart';
import 'package:videotop/widget/choosesex.dart';
import 'package:videotop/widget/home.dart';
import 'package:regexed_validator/regexed_validator.dart';

import '../util.dart';

/// created by hx
///    2021/1/23:16:58

class RegEmailModel extends BaseModel{

  Util util;
  BuildContext context;
  RegEmailModel(this.context,this.util);

  String email = '';
  String psd = '';
  bool eyes = true;

  setEmail(String value){
    this.email = value;
  }

  setPsd(String value){
    this.psd = value;
  }

  setEyes(){
    if (eyes) {
      eyes = false;
    } else {
      eyes = true;
    }
    notifyListeners();
  }

  login() async{
    FocusScope.of(context).requestFocus(FocusNode());
    util.waiting(context);
    if(validator.email(email)){
      if(psd.length >= 6){
        util.login(1, email, psd).then((value) {
          Navigator.pop(context);
          LoginBean loginBean = value;
          if(loginBean.status == 1){
            Navigator.pushAndRemoveUntil(
                context,
                new MaterialPageRoute(builder: (context) => new Home()),
                    (route) => route == null);
          }else if(value == -101){
            util.showToast('The account has been deleted');
          }else {
            Navigator.push(
                context,
                new MaterialPageRoute(
                    builder: (context) => new ChooseSex(
                      type: 1,
                      logid: email,
                      psd: psd,
                    )));
          }
        });
      }else{
        Navigator.pop(context);
        util.showToast('Please input your psd');
      }
    }else{
      Navigator.pop(context);
      util.showToast('Invalid email format');
    }
  }
}